package com.itheima.web;

import com.itheima.base.BaseServlet;
import com.itheima.pojo.Contact;
import com.itheima.service.ContactService;
import com.itheima.service.findAllService;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author MaQing
 * @create 2022-05-12 09:41
 */
@WebServlet("/contact")
public class ContactServlet extends BaseServlet {

    //分页查询
    public void findByPage (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        //代码的健壮性
        //1.获取请求携带的参数  pageSize currentPage
        int pageSize = 3;
        int currentPage = 1;

        try{
            String pageSize1 = request.getParameter("pageSize");
            pageSize = Integer.parseInt(pageSize1);
            if (pageSize<=0){
                pageSize=3;
            }

        }catch (Exception e){
            pageSize = 3;
        }

        try{
            String currentPage1 = request.getParameter("currentPage");
            currentPage = Integer.parseInt(currentPage1);
            if (currentPage<0){
                currentPage = 1;
            }

        }catch (Exception e){
            currentPage = 1;
        }

        //2.调用service处理业务逻辑
        ContactService service = new ContactService();
        List<Contact> list = service.findByPage(pageSize,currentPage);
        //3.将查结果存放到request域中,请求转发到pageList.jsp页面上展示
        request.setAttribute("list",list);
        request.getRequestDispatcher("/contact/pageList.jsp").forward(request,response);
    }




    //多条件查询
    public void findByCondition(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        //1.获取请求携带的参数信息
        String name = request.getParameter("name");
        String qq = request.getParameter("qq");
        String min = request.getParameter("min");
        String max = request.getParameter("max");
        //2.封装数据到map集合
        Map<String,String> map = new HashMap();
        map.put("name",name);
        map.put("qq",qq);
        map.put("min",min);
        map.put("max",max);
        System.out.println("查询条件为:"+map);
        //3.调用service处理业务逻辑
        ContactService service = new ContactService();
        List<Contact> list = service.findByCondition(map);
        //4.请求转发到list.jsp页面上展示查询结果
        request.setAttribute("list",list);
        request.getRequestDispatcher("/contact/list.jsp")
                .forward(request,response);

    }


    //删除选中的联系人的信息
    public void deleteAll(HttpServletRequest request, HttpServletResponse response) throws IOException {

        //todo: 判断执行结果的方式有多种
        // 返回boolean值
        // throw Exception 效率高
        try {
            //1.获取请求携带的ids值
            String ids[] = request.getParameterValues("ids");
            System.out.println("要删除的联系人的id为："+ Arrays.toString(ids));
            //2.调用service处理业务逻辑
            ContactService service = new ContactService();
            service.deleteAll(ids);
            //3.重定向查询所有联系人
            response.sendRedirect(request.getContextPath()+"/contact?method=findAll");

        } catch (Exception e) {
            e.printStackTrace();
            //删除失败
        }

    }


//    根据id进行删除联系人信息
    public void deleteContactById(HttpServletRequest request, HttpServletResponse response) throws IOException {
        //1.获取请求携带的id值
        String id = request.getParameter("id");

        //2.调用service处理业务逻辑

        ContactService service = new ContactService();
        boolean flag =service.deleteContactById(id);

        if (flag){
            // 修改成功
            // 重定向查询所有联系人信息
            response.sendRedirect(request.getContextPath()+"/contact?method=findAll");

        }

    }

    //根据id修改联系人的信息
    public void updateContactById(HttpServletRequest request, HttpServletResponse response) {
        try {
            Map<String, String[]> map = request.getParameterMap();

            Contact contact = new Contact();
            BeanUtils.populate(contact,map);
            // 3.调用service处理业务逻辑

            ContactService service = new ContactService();
            boolean flag = service.updateContact(contact);

            //判断结果
            if (flag){
                //修改成功
                response.sendRedirect(request.getContextPath()+"/contact?method=findAll");
            }else{
                //修改失败
                request.setAttribute("msg","当前功能正在维护");
                //转发到修改页面
                request.getRequestDispatcher("/contact/update.jsp").forward(request,response);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    //根据id查询联系人的信息
    public void findContactById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String id = request.getParameter("id");
        //调用service业务

        ContactService service = new ContactService();
        Contact contact = service.findContactById(id);

        //结果放在域中,共享到update.jsp页面
        request.setAttribute("contact",contact);
        request.getRequestDispatcher("/contact/update.jsp").forward(request,response);

    }

    //添加联系人
    public void addContact(HttpServletRequest request, HttpServletResponse response) {
        try {
            //获取参数
            Map<String, String[]> map = request.getParameterMap();

            Contact contact = new Contact();
            //封装
            BeanUtils.populate(contact,map);
            System.out.println("添加的信息为："+contact);
            //servcie
            ContactService service = new ContactService();
            Boolean flag = service.addContact(contact);

            if (flag){
                //添加成功后，重定向到findAll
                response.sendRedirect(request.getContextPath()+"/contact?method=findAll");
            }else{
                //失败后
                request.setAttribute("msg","当前功能正在维护哦");
                request.getRequestDispatcher("/contact/add.jsp").forward(request,response);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //查询所有的联系人
    public void findAll(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //service处理业务
        findAllService service = new findAllService();
        List<Contact> list = service.findAll();

        request.setAttribute("list",list);
        request.getRequestDispatcher("/contact/list.jsp").forward(request,response);
    }


}